import Vue from 'vue'
import Vuex from 'vuex'
import { setItem, getItem, removeItem } from '@/utils/storage.js'
Vue.use(Vuex)
// 登陆成功，将Token存储到Vuex容器中（为了获取方便和响应式）
// 为了持久化，还需要把Token放到本地存储（为了持久化）
const TOKEN_KEY = 'TOUTIAO_USER'
export default new Vuex.Store({
  state: {
    user: getItem(TOKEN_KEY)
  },
  mutations: {
    setUser (state, data) {
      state.user = data
      setItem(TOKEN_KEY, state.user)
    },
    removeUser (state) {
      removeItem(TOKEN_KEY)
      state.user = getItem(TOKEN_KEY)
    }
  },
  actions: {},
  modules: {}
})
